/*********************************************************
          File Name:16.63.64cpp
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Sat 07 May 2016 01:33:49 PM CST
**********************************************************/

#include <iostream>
#include <vector>
#include <cstring>

template<typename T>
auto count(const T needle, std::vector<T>& v) -> typename std::vector<T>::size_type
{
  typename std::vector<T>::size_type res = 0;
  for(typename std::vector<T>::iterator it = v.begin(); it != v.end(); ++it)
  {
    if(needle == *it)
      res += 1;
  }

  return res;
}

template<> 
auto count(const char *needle, std::vector<const char *>& v) -> std::vector<const char *>::size_type
{
  std::vector<const char *>::size_type res = 0;
  for(std::vector<const char *>::iterator it = v.begin(); it != v.end(); ++it)
  {
    if(strcmp(needle, *it) == 0)
      res += 1;
  }

  return res;
}

int main()
{
  std::vector<double> vd{0.1, 0.2, 0.3, 0.2, 0.2};
  std::vector<int> vi{1, 2, 3, 3, 3, 1};
  std::vector<std::string> vs{"one", "two", "one", "three", "one"};

  std::cout << 0.2 << " appears: " << count(0.2, vd) << " times\n";
  std::cout << 3 << " appears: " << count(3, vi) << " times\n";
  std::cout << "'two' appears: " << count(std::string("two"), vs) << " time\n";

  // 16.64
  // explicitly convert c-style string to std::string
  std::vector<const char *> vc{"one", "two", "three"};
  std::cout << "one apears: " << count("one", vc) << " times\n"; 
}
